/*
** game_func.c for Game_func in /home/vriely/Dropbox/work/Unix/Zappy_rendu/serv-src
**
** Made by Samuel Vrielynck
** Login   <vriely_s@epitech.net>
**
** Started on  Mon Jun 11 17:25:51 2012 Samuel Vrielynck
** Last update Wed Jul 18 19:36:51 2012 Samuel Vrielynck
*/

#include			<string.h>
#include			<stdio.h>
#include			<stdlib.h>

#include			"server.h"

static const t_inven_func	tab[] =
{
  { "nourriture", FOOD },
  { "linemate", LINEMATE },
  { "deraumere", DERAUMERE },
  { "sibur", SIBUR },
  { "mendiane", MENDIANE },
  { "phiras", PHIRAS },
  { "thystame", THYSTAME },
  { NULL, -1 }
};

static void			send_data(t_game *game, \
			t_client *client, char *type, int i)
{
  if (game->graphic != NULL)
    {
      game->graphic->send->concat(game->graphic->send, type);
      game->graphic->send->concat(game->graphic->send, " ");
      game->graphic->send->concat_value(game->graphic->send, client->sock);
      game->graphic->send->concat(game->graphic->send, " ");
      game->graphic->send->concat_value(game->graphic->send, i);
      game->graphic->send->concat(game->graphic->send, "\n");
      serv_monitor("pin", client, game, game->graphic);
      serv_monitor("bct", client, game, game->graphic);
    }
}

char				*connect_nbr(t_client *client, t_game *game)
{
  char				*msg;
  struct s_item			*cur;

  msg = NULL;
  cur = game->team->head;
  client->gamer->action = 1;
  while (cur != NULL)
  {
    if (((struct s_team *)cur->content)->id == client->gamer->team->id)
    {
      msg = (char *)malloc(12);
      if (msg != NULL)
        sprintf(msg, "%d\n", ((struct s_team *)cur->content)->slot);
      break;
    }
    cur = cur->next;
  }
  return (msg);
}

char				*inventaire(t_client *client, t_game *game)
{
  char				*msg;

  client->gamer->action = MILLI / game->time;
  msg = (char *)malloc(256);
  if (msg != NULL)
  {
    sprintf(msg, "{nourriture %d, linemate %d, deraumere %d, sibur %d",
	client->gamer->inventory[FOOD], client->gamer->inventory[LINEMATE],
	client->gamer->inventory[DERAUMERE], client->gamer->inventory[SIBUR]);
    sprintf(msg + strlen(msg), ", mendiane %d, phiras %d, thystamei %d}\n",
	client->gamer->inventory[MENDIANE], client->gamer->inventory[PHIRAS],
	client->gamer->inventory[THYSTAME]);
  }
  return (msg);
}

char				*pose(t_client *client, t_game *game)
{
  char				*res;
  int				i;
  char				*object;

  strtok((char*)client->gamer->task->head->content, " ");
  res = NULL;
  if ((object = strtok(NULL, " ")) != NULL)
  {
    i = 0;
    client->gamer->action = 7 * MILLI / game->time;
    while (tab[i].name != NULL)
    {
      if (!strcmp(object, tab[i].name))
      {
	res = gere_ressource(client->gamer, game, 1, tab[i].type);
	if (!strcmp(res, "ok\n"))
	  send_data(game, client, "pdr", i);
      }
      i++;
    }
  }
  return (res);
}

char				*prend(t_client *client, t_game *game)
{
  char				*res;
  int				i;
  char				*object;

  strtok((char*)client->gamer->task->head->content, " ");
  res = NULL;
  if ((object = strtok(NULL, " ")) != NULL)
  {
    i = 0;
    client->gamer->action = 7 * MILLI / game->time;
    while (tab[i].name != NULL)
    {
      if (!strcmp(object, tab[i].name))
      {
	res = gere_ressource(client->gamer, game, 0, tab[i].type);
	if (!strcmp(res, "ok\n"))
	  send_data(game, client, "pgt", i);
      }
      i++;
    }
  }
  return (res);
}
